﻿/*
 * @Autor: PK
 * @Date: 2022/01/04 14:35:SS
 */

using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using ShkSimulation.desktop.setting.tools;

namespace ShkSimulation.desktop.setting {

	public class ConfigManager {
		
		public static SimulationConfig UserSimulationConfig { get; set; }
		private static SimulationConfig DefaultSimulationConfig { get; set; }
		
		public static RunConfig UserRunConfig { get; set; }
		private static RunConfig DefaultRunConfig { get; set; }
		
		public static SystemConfig UserSystemConfig { get; set; }
		private static SystemConfig DefaultSystemConfig { get; set; }

		public static void SaveSimulationConfig() {
			ConfigHelper.Save(UserSimulationConfig, SimulationSettings.Default);
			DefaultSimulationConfig = ConfigHelper.LoadConfig<SimulationConfig>(SimulationSettings.Default);
		}
		
		public static void SaveRunConfig() {
			ConfigHelper.Save(UserRunConfig, RunSettings.Default);
			DefaultRunConfig = ConfigHelper.LoadConfig<RunConfig>(RunSettings.Default);
		}
		
		public static void SaveSystemConfig() {
			ConfigHelper.Save(UserSystemConfig, SystemSettings.Default);
			DefaultSystemConfig = ConfigHelper.LoadConfig<SystemConfig>(SystemSettings.Default);
		}
		
		public static void ResetSystemConfig() {
			SystemSettings.Default.Reset();
			DefaultSystemConfig = ConfigHelper.LoadConfig<SystemConfig>(SystemSettings.Default);
			UserSystemConfig = ConfigHelper.LoadConfig<SystemConfig>(SystemSettings.Default);
			UserSystemConfig.PropertyChanged += (_, _) => SaveSystemConfig();
		}
		
		public static void ResetRunConfig() {
			RunSettings.Default.Reset();
			DefaultRunConfig = ConfigHelper.LoadConfig<RunConfig>(RunSettings.Default);
			UserRunConfig = ConfigHelper.LoadConfig<RunConfig>(RunSettings.Default);
			UserRunConfig.PropertyChanged += (_, _) => SaveRunConfig();
		}
		
		static ConfigManager() {
			// Simulation
			DefaultSimulationConfig = ConfigHelper.LoadConfig<SimulationConfig>(SimulationSettings.Default);
			UserSimulationConfig = ConfigHelper.LoadConfig<SimulationConfig>(SimulationSettings.Default);
			// Simulation Run
			DefaultRunConfig = ConfigHelper.LoadConfig<RunConfig>(RunSettings.Default);
			UserRunConfig = ConfigHelper.LoadConfig<RunConfig>(RunSettings.Default);
			// System
			DefaultSystemConfig = ConfigHelper.LoadConfig<SystemConfig>(SystemSettings.Default);
			UserSystemConfig = ConfigHelper.LoadConfig<SystemConfig>(SystemSettings.Default);
			
			UserSimulationConfig.PropertyChanged += (_, _) => SaveSimulationConfig();
			UserRunConfig.PropertyChanged += (_, _) => SaveRunConfig();
			UserSystemConfig.PropertyChanged += (_, _) => SaveSystemConfig();
			
		}

		private readonly static string Spliter = @"-shk-";

		public static void BackupConfigsByJson(string backupFilePath) {
			if (!File.Exists(backupFilePath)) {
				File.Delete(backupFilePath);
			}
			JsonSerializer serializer = new JsonSerializer();
			using FileStream fileStream = File.OpenWrite(backupFilePath);
			using TextWriter textWriter = new StreamWriter(fileStream);
			using JsonWriter writer = new JsonTextWriter(textWriter);
			
			string systemString = System.Text.Json.JsonSerializer.Serialize(UserSystemConfig);
			string runString = System.Text.Json.JsonSerializer.Serialize(UserRunConfig);
			string simulationString = System.Text.Json.JsonSerializer.Serialize(UserSimulationConfig);
			string configData = $"{systemString}{Spliter}{runString}{Spliter}{simulationString}";
			serializer.Serialize(writer, configData);
		}
		
		public static void LoadConfigsFromJson(string backupFilePath) {
			if (!File.Exists(backupFilePath)) {
				MessageBox.Show(@"文件不存在", @"提示");
			}
			JsonSerializer serializer = new JsonSerializer();
			using FileStream fileStream = File.OpenRead(backupFilePath);
			using TextReader textReader = new StreamReader(fileStream);
			using JsonReader reader = new JsonTextReader(textReader);
			string configData = serializer.Deserialize<string>(reader)?.Trim();
			string[] data = configData?.Split(Spliter);
			if (data == null || data.Length != 3) {
				MessageBox.Show(@"读取配置错误", @"提示");
				return;
			}
			UserSystemConfig = System.Text.Json.JsonSerializer.Deserialize<SystemConfig>(data[0]);
			UserRunConfig = System.Text.Json.JsonSerializer.Deserialize<RunConfig>(data[1]);
			UserSimulationConfig = System.Text.Json.JsonSerializer.Deserialize<SimulationConfig>(data[2]);
		}

	}

}